package com.example.demo;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;

import java.util.ArrayList;
import java.util.List;

public class FragmentAdapter extends FragmentStateAdapter {

    // Fragment列表
    private final List<Fragment> fragmentList = new ArrayList<>();
    // 标题列表
    private final List<String> titleList = new ArrayList<>();

    public FragmentAdapter(@NonNull FragmentActivity fragmentActivity) {
        super(fragmentActivity);

        // 添加Fragment和对应的标题
        fragmentList.add(new HomeFragment());
        fragmentList.add(new CategoryFragment());
        fragmentList.add(new ProfileFragment());

        titleList.add("首页");
        titleList.add("分类");
        titleList.add("我的");
    }

    @NonNull
    @Override
    public Fragment createFragment(int position) {
        // 返回对应位置的Fragment
        return fragmentList.get(position);
    }

    @Override
    public int getItemCount() {
        // 返回Fragment数量
        return fragmentList.size();
    }

    // 获取标题（用于TabLayout）
    public String getTitle(int position) {
        return titleList.get(position);
    }
}